/**
 * Copyright 2005 The Apache Software Foundation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.edi.runtime.component
{
	import flash.events.ContextMenuEvent;
	import flash.ui.ContextMenu;
	import flash.ui.ContextMenuItem;
	
	/**
	 * 添加菜单
	 * var menu:EDIContextMenu;
	 * var menuSearch:ContextMenuItem;
	 * menu = new EDIContextMenu();
	 * menuSearch=menu.addSubmenu("查找..",search,true,false,true);	
	 * this.dataList.contextMenu = menu.contextMenu; 
	 * public function search(event:ContextMenuEvent):void
	 * {
	 *	...
	 * }
	 * 
	 */	
	public class EDIContextMenu
	{
		private var _contextMenu:ContextMenu;
		
		/**
		 * 构造 EDIContextMenu对象，并封装ContextMenu对象 
		 * 
		 */		
		public function EDIContextMenu()
		{
			_contextMenu = new ContextMenu();
			_contextMenu.hideBuiltInItems();
		}
		
		/**
		 * 添加一个新的菜单item项 
		 * @param caption  菜单标题。
		 * @param listen   点击后触发的事件
		 * @param enable   设置菜单的enable属性
		 * @param useCapture  
		 * @param separatorBefore 设置分割线
		 * @return 
		 * 
		 */		
		public function addSubmenu(caption:String,listen:Function,enable:Boolean=true,useCapture:Boolean=false,separatorBefore:Boolean=false):ContextMenuItem{
			var cmi:ContextMenuItem;
			cmi=new ContextMenuItem(caption,separatorBefore,enable);
			cmi.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, listen,useCapture);
			this._contextMenu.customItems.push(cmi);
			
			
			cmi.enabled = enable;
			return cmi;
		}
		
		/**
		 * 返回 ContextMenu 对象
		 * @return 
		 * 
		 */		
		public function get contextMenu():ContextMenu{
			return this._contextMenu;
		}
		
		/**
		 * 移除子菜单项 
		 * @param caption
		 * 
		 */		
		public function removeSubmenu(caption:String):void{
			var items:Array = this._contextMenu.customItems;
			for(var i:int=items.length-1;i>=0;i--){
				var cmi:ContextMenuItem = items[i] as ContextMenuItem;
				if (cmi.caption == caption){
					items.splice(i,1);
					break;
				}
			}
		}
		
		/**
		 *  根据菜单名称，设置该菜单的enable属性
		 * @param caption
		 * 
		 */		
		public function disableSubmeu(caption:String):void{
			setMenuEnable(caption,false);
		}
		
		/**
		 *  根据菜单名称，设置该菜单的enable属性
		 * @param caption
		 * 
		 */	
		public function enableSubMenu(caption:String):void{
			setMenuEnable(caption,true);
		}
		
		/**
		 *  根据caption名称，设置该菜单的enable属性
		 * @param caption 菜单名
		 * @param enable 设置enable属性。
		 * 
		 */		
		private function setMenuEnable(caption:String,enable:Boolean):void{
			var items:Array = this._contextMenu.customItems;
			for(var i:int=items.length-1;i>=0;i--){
				var cmi:ContextMenuItem = items[i] as ContextMenuItem;
				if (cmi.caption == caption){
					cmi.enabled = enable;
					break;
				}
			}
		}

	}
}